<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/8
 * Time: 14:23
 */

namespace Game\Services;
use GatewayWorker\Lib\Gateway;
use Game\Lib\Game;

use Game\Traits\Singleton;

class GatewayService
{
    use Singleton;
    public function pushMessageToGroup($room_id, $msg_arr, $exclude_client_id=null){
        $msg = Tools::getInstance()->_JSON($msg_arr);
        Gateway::sendToGroup($room_id, $msg, $exclude_client_id);
    }
    public function pushMessageToAccount($room_id, $account_id, $msg_arr){
        $replyArr = array("[roomid]"=>$room_id,"[accountid]"=>$account_id);
        $roomUserUID = strtr(Game::RoomUser_UID, $replyArr);
        $msg = Tools::getInstance()->_JSON($msg_arr);
        Gateway::sendToUid($roomUserUID, $msg);
    }
    public function pushMessageToCurrentClient($msg_arr){
        $msg = Tools::getInstance()->_JSON($msg_arr);
        Gateway::sendToCurrentClient($msg);
    }

    public function queryOnlineStatus($room_id, $account_id){
        $roomUserUID = strtr(Game::RoomUser_UID, array("[roomid]"=>$room_id,"[accountid]"=>$account_id));
        return Gateway::isUidOnline($roomUserUID);
    }
    public function getClientSessionsByGroup($room_id){
        return Gateway::getClientSessionsByGroup($room_id);
    }

    /**
     * 判断请求链接合法性
     * @param $client_id
     * @param $room_id
     * @param $account_id
     * @return bool
     */
    public function checkRequestClientLegal($client_id,$room_id,$account_id)
    {
        return true;
        //绑定用户UID
        $replyArr = array("[roomid]"=>$room_id,"[accountid]"=>$account_id);
        $room_aid = strtr(Game::RoomUser_UID, $replyArr);

        $client_array = Gateway::getClientIdByUid($room_aid);
        if(is_array($client_array) && count($client_array) > 0) {
            foreach($client_array as $bind_client) {
                if($bind_client == $client_id) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 
     * @param $room_id
     * @param $noty_arr
     * @param null $exclude_client_id
     * @return bool
     */
    public function notyUpdateAccountStatusToGroup($room_id, $noty_arr, $exclude_client_id=null){
        if(!isset($noty_arr['account_id']) || !isset($noty_arr['account_status']) ){
            $this->writeLog("function(notyUpdateAccountStatusToGroup):lack of params"." in file".__FILE__." on Line ".__LINE__);
            return false;
        }

        $msg_arr = array("result"=>"0","operation"=>"UpdateAccountStatus","data"=>$noty_arr,"result_message"=>"用户状态改变");
        $this->pushMessageToGroup($room_id, $msg_arr, $exclude_client_id=null);
    }


    /**
     * 绑定用户UID到房间
     * @param $client_id
     * @param $room_id
     * @param $account_id
     */
    public function joinGatewayRoom($client_id, $room_id, $account_id){
        $roomUserUID = strtr(Game::RoomUser_UID, ['[roomid]'=>$room_id, '[accountid]'=>$account_id]);
        Gateway::bindUid($client_id, $roomUserUID);
        Gateway::joinGroup($client_id, $room_id);
    }

}